﻿using System;
using System.Configuration;
using System.IO;
using System.Web.Configuration;
using System.Web.Mvc;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// ProtectConfigurationController
    /// </summary>
    public class ProtectConfigurationController : Controller
    {
        const string PROVIDER = "RSAProtectedConfigurationProvider";
        const string SECTION = "connectionStrings";

        /// <summary>
        /// Provide Encrypted Configuration Settings
        /// </summary>
        /// <param name="submitButton"></param>
        /// <returns></returns>
        public ActionResult Index(string submitButton)
        {
            switch (submitButton)
            {
                case "Encrypt":
                    //encryptConnectionString();
                    EncryptSection("connectionStrings", "DataProtectionConfigurationProvider");
                    ViewData["WebConnectionString"] = loadConnectionString();
                    return View();
                case "Decrypt":
                    //decryptConnectionString();
                    DecryptSection("connectionStrings");
                    ViewData["WebConnectionString"] = loadConnectionString();
                    return View();
                default:
                    ViewData["WebConnectionString"] = loadConnectionString();
                    return (View());
            }
        }

        /// <summary>
        /// Encrypt Section
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="provider"></param>
        public void EncryptSection(string sectionName, string provider)
        {
            var cfg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            if (cfg == null)
                return;
            var section = cfg.GetSection(sectionName);
            if (section == null)
                return;
            if (section.SectionInformation.IsProtected) return;
            section.SectionInformation.ProtectSection(provider);
            cfg.Save();
        }

        /// <summary>
        /// Decrypt Section
        /// </summary>
        /// <param name="sectionName"></param>
        public void DecryptSection(string sectionName)
        {
            var cfg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            if (cfg == null)
                return;
            var section = cfg.GetSection(sectionName);
            if (section == null)
                return;
            if (!section.SectionInformation.IsProtected) return;
            section.SectionInformation.UnprotectSection();
            cfg.Save();
        }




        private void encryptConnectionString()
        {
            var confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);   
            var confStrSect = confg.GetSection(SECTION);   
            if (confStrSect != null)   
            {   
                confStrSect.SectionInformation.ProtectSection(PROVIDER);   
                confg.Save();   
            }
        }

        private void decryptConnectionString()
        {
            var confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);   
            var confStrSect = confg.GetSection(SECTION);   
            if (confStrSect != null && confStrSect.SectionInformation.IsProtected)   
            {   
                confStrSect.SectionInformation.UnprotectSection();   
                confg.Save();   
            }
        }

        private string loadConnectionString()
        {
            var objConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection objConfigSection = objConfig.ConnectionStrings;
            var connectionString = WebConfigurationManager.GetSection(SECTION).ToString();
            return connectionString;
        }
    }
}